package com.cetc.controller;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by shea on 2018/10/19.
 */
@Controller
public class UserController {

    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setMaxInactiveInterval(1000*60*60);
        System.out.println(session.getId());
        String username=request.getParameter("username");

        if (session.getAttribute("user") == null) {
            //进行登录
            if (username.equals("user")) {
                session.setAttribute("user1",session.getId());
                return "登录成功！";
            }else{
                return "登录失败！";
            }
        } else if (session.getAttribute("user1") != null && session.getAttribute("user1").equals(session.getId())) {
            return "免登陆";
        }
        return "异常";
    }
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setMaxInactiveInterval(1000*60*60);
        System.out.println(session.getId());

        if (session.getAttribute("user1")!=null && session.getAttribute("user1").equals(session.getId())) {
            session.removeAttribute("user1");
            return "用户已退出";
        }
        return "用户已退出";
    }

}
